package com.shu.driftbook.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.hibernate.engine.Mapping;

import com.opensymphony.xwork2.ActionSupport;
import com.shu.driftbook.dao.Book;
import com.shu.driftbook.service.BookService;

public class SearchAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	private BookService service;
	private String nameorisbn;
	private javax.servlet.http.HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	private HttpServletResponse response;
	private List<Book> result;
	public BookService getService() {
		return service;
	}

	public void setService(BookService service) {
		this.service = service;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request=arg0;
		this.session=request.getSession();
		this.application=session.getServletContext();
	}
	
	public String executetool(){
		result=service.findTool();
		return SUCCESS;
	}
	
	public String executenovel(){
		result=service.findNovel();
		return SUCCESS;
	}
	
	public String execute() throws IOException{
		result=service.find(nameorisbn);
//		PrintWriter out=response.getWriter();
//		response.setContentType("text/html;charset=UTF-8");
//		response.setContentType("text/html");
//        response.setHeader("Cache-Control", "no-store");  
//        response.setHeader("Pragma", "no-cache");  
//        response.setDateHeader("Expires", 0);
//        setResult(service.find(nameorisbn));
//        for(int i=0;i<getResult().size();i++){
//        	out.write("<div>");
//        	out.write("<ul>");
//        	out.write("<li>"+getResult().get(i).getBookname()+"</li>");
//        	out.write("<li>"+getResult().get(i).getAuthor()+"</li>");
//        	out.write("<li>"+getResult().get(i).getISBN()+"</li>");
//        	out.write("<li>"+getResult().get(i).getPublisher()+"</li>");
//        	out.write("<li>"+getResult().get(i).getDonator()+"</li>");
//        	out.write("</ul");
//        	out.write("</div>");
//        }
//        out.close();
		return SUCCESS;
	}
	
	public String getNameorisbn() {
		return nameorisbn;
	}

	public void setNameorisbn(String nameorisbn) {
		this.nameorisbn = nameorisbn;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response=arg0;
	}

	public List<Book> getResult() {
		return result;
	}

	public void setResult(List<Book> result) {
		this.result = result;
	}
}
